package com.qianying.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianying.comment.CommonResult;
import com.qianying.entity.PlatformTimeliness;
import com.qianying.mapper.PlatformTimelinessMapper;
import com.qianying.service.PlatformTimelinessService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 平台时效表Service实现类
 * 包含字段校验和业务逻辑处理
 *
 * @author check
 * @date 2025-08-02
 */
@Service
public class PlatformTimelinessServiceImpl extends ServiceImpl<PlatformTimelinessMapper, PlatformTimeliness> implements PlatformTimelinessService {

    /**
     * 分页查询（支持按时效类型模糊搜索）
     */
    @Override
    public CommonResult<IPage<PlatformTimeliness>> getPage(Page<PlatformTimeliness> page, String timelinessType) {
        // 构建查询条件
        LambdaQueryWrapper<PlatformTimeliness> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(timelinessType)) {
            queryWrapper.like(PlatformTimeliness::getTimelinessType, timelinessType);
        }
        // 执行分页查询
        IPage<PlatformTimeliness> resultPage = baseMapper.selectPage(page, queryWrapper);
        return CommonResult.success(resultPage);
    }

    /**
     * 按ID查询
     */
    @Override
    public CommonResult<PlatformTimeliness> getById(Long id) {
        // 校验ID
        if (id == null || id <= 0) {
            return CommonResult.failed("ID无效，请传入正整数");
        }
        // 查询数据
        PlatformTimeliness timeliness = baseMapper.selectById(id);
        if (timeliness == null) {
            return CommonResult.failed("未找到ID为" + id + "的时效记录");
        }
        return CommonResult.success(timeliness);
    }

    /**
     * 新增（含字段校验）
     */
    @Override
    public CommonResult<?> saveTimeliness(PlatformTimeliness timeliness) {
        // 字段非空校验
        if (!StringUtils.hasText(timeliness.getTimelinessType())) {
            return CommonResult.failed("时效类型不能为空");
        }
        if (!StringUtils.hasText(timeliness.getStartNode())) {
            return CommonResult.failed("开始节点不能为空");
        }
        if (!StringUtils.hasText(timeliness.getEndNode())) {
            return CommonResult.failed("结束节点不能为空");
        }
        // 时间有效性校验（必须为正数）
        if (timeliness.getUrgentTime() == null || timeliness.getUrgentTime() <= 0) {
            return CommonResult.failed("紧急单处理时间必须为正数");
        }
        if (timeliness.getNormalTime() == null || timeliness.getNormalTime() <= 0) {
            return CommonResult.failed("普通单处理时间必须为正数");
        }
        // 执行新增
        boolean save = save(timeliness);
        return save ? CommonResult.success("新增成功") : CommonResult.failed("新增失败");
    }

    /**
     * 更新（含字段校验）
     */
    @Override
    public CommonResult<?> updateTimeliness(PlatformTimeliness timeliness) {
        // ID校验
        if (timeliness.getId() == null || timeliness.getId() <= 0) {
            return CommonResult.failed("ID无效，请传入正整数");
        }
        // 校验记录是否存在
        PlatformTimeliness exist = baseMapper.selectById(timeliness.getId());
        if (exist == null) {
            return CommonResult.failed("未找到ID为" + timeliness.getId() + "的时效记录，无法更新");
        }
        // 字段非空校验（同新增）
        if (!StringUtils.hasText(timeliness.getTimelinessType())) {
            return CommonResult.failed("时效类型不能为空");
        }
        if (!StringUtils.hasText(timeliness.getStartNode())) {
            return CommonResult.failed("开始节点不能为空");
        }
        if (!StringUtils.hasText(timeliness.getEndNode())) {
            return CommonResult.failed("结束节点不能为空");
        }
        // 时间有效性校验
        if (timeliness.getUrgentTime() == null || timeliness.getUrgentTime() <= 0) {
            return CommonResult.failed("紧急单处理时间必须为正数");
        }
        if (timeliness.getNormalTime() == null || timeliness.getNormalTime() <= 0) {
            return CommonResult.failed("普通单处理时间必须为正数");
        }
        // 执行更新
        boolean update = updateById(timeliness);
        return update ? CommonResult.success("更新成功") : CommonResult.failed("更新失败");
    }

    /**
     * 删除（含ID校验）
     */
    @Override
    public CommonResult<?> removeById(Long id) {
        // ID校验
        if (id == null || id <= 0) {
            return CommonResult.failed("ID无效，请传入正整数");
        }
        // 校验记录是否存在
        PlatformTimeliness exist = baseMapper.selectById(id);
        if (exist == null) {
            return CommonResult.failed("未找到ID为" + id + "的时效记录，无法删除");
        }
        // 执行删除
        int removeNum = baseMapper.deleteById(id);
        return removeNum>0 ? CommonResult.success("删除成功") : CommonResult.failed("删除失败");
    }
}